<?php
/**
 * Created by JetBrains PhpStorm.
 * User: xx
 * Date: 14-7-24
 * Time: 下午3:44
 * To change this template use File | Settings | File Templates.
 * 作业模型
 */

if (!function_exists("fun_usort")) {
    function fun_usort($a, $b)
    {
        if ($a['wait_num'] == $b['wait_num']) {
            return 0;
        }
        else {
            return ($a['wait_num'] < $b['wait_num']) ? 1 : -1;
        }
    }
}

class work_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();

        /**
         * 对模块的返回值进行扩展
         * 6000-7000 目录模型使用
         */
        $this->return_status[6001] = "没有作业数据";
        $this->return_status[6002] = "";
    }

    public function get_work_data($work_id)
    {
        $ret = new Ret($this);
        $work = $this->get_work_by_id($work_id);
        $work_info = $this->get_work_info($work_id);
        if ($work && $work_info) {
            $work['info'] = $work_info;
            $ret->set_code(0);
            $ret->set_data($work);
        } else {
            $ret->set_code(6001);
        }
        return $ret;
    }

    /**
     * $row = $query->row_array(); result_array
     * @param $work_id
     * @return bool
     */
    private function get_work_by_id($work_id)
    {
        $rows = FALSE;
        $sql = "SELECT * FROM " . XS_WORK . " WHERE work_id = ?";
        $query = $this->db->query($sql, array($work_id));
        if ($query->num_rows() > 0) {
            $rows = $query->row_array();
        }

        return $rows;
    }

    /**
     * 作业明细
     * @param $work_id
     * @return bool
     */
    private function get_work_info($work_id)
    {
        $rows = FALSE;
        $sql = "SELECT * FROM " . XS_WORKINFO . " WHERE work_id = ?";
        $query = $this->db->query($sql, array($work_id));
        if ($query->num_rows() > 0) {
            $rows = $query->result_array();
        }

        return $rows;
    }

    public function add_work($data)
    {

        $work_id = 0;
        $work_infos = $data['spec'];
        unset($data['spec']);

        $this->db->insert(XS_WORK, $data);
        $work_id = $this->db->insert_id();
        if ($work_id > 0) {
            foreach ($work_infos as $work_info) {
                $work_info['work_id'] = $work_id;
                $this->add_work_info($work_info);
            }
        }
        return $work_id;
    }

    public function add_work_info($work_info)
    {
        $this->db->insert(XS_WORKINFO, $work_info);
    }

    public function edit_work($work_id, $data)
    {
        $this->db->where('work_id', $work_id);
        $this->db->update(XS_WORK, $data);
    }

    /**
     * 已发货计数号
     */
    public function deliver_goods()
    {
        $today_start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $sql = "SELECT work_counter FROM " . XS_WORK . " WHERE add_time >= ? AND work_status = 4 ORDER BY work_counter DESC";//
        $query = $this->db->query($sql, array($today_start));
        $rows_tmp = array();
        if ($query->num_rows() > 0) {
            $rows_tmp = $query->result_array();
        }

        return $rows_tmp;
    }

    /**
     * 获取统计数据
     * @param $pipe_name
     * @return array
     */
    public function get_statistical($pipe_name)
    {
        $today_start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $rows = array(
            'all' => 0,
        );
        $rows[$pipe_name] = 0;

        $sql = "SELECT count(*) sum FROM " . XS_WORK . " WHERE add_time >= ? AND work_pipe = ?"; //work_status < 4 AND
        $query = $this->db->query($sql, array($today_start, $pipe_name));
        if ($query->num_rows() > 0) {
            $rows_tmp = $query->row_array();
            $rows[$pipe_name] = $rows_tmp['sum'];
        }

        $sql = "SELECT count(*) sum FROM " . XS_WORK . " WHERE add_time >= ?"; //work_status < 4
        $query = $this->db->query($sql, array($today_start));
        if ($query->num_rows() > 0) {
            $rows_tmp = $query->row_array();
            $rows['all'] = $rows_tmp['sum'];
        }

        return $rows;
    }

    public function update_status($work_id, $old_status, $new_status, $time, $work_pipe = '')
    {
        $data = array(
            'work_status' => $new_status,
        );

        switch ($old_status) {
            case 1: //入排队
                $data['wl_end'] = $time;
                break;
            case 2: //入流水队
                $data['pl_end'] = $time;
                break;
            case 3: //挂起
                $data['hu_end'] = $time;
                break;
            case 4: //完成
            case 5: //作费
                break;
            default:
                break;
        }

        switch ($new_status) {
            case 1: //入排队
                $data['wl_start'] = $time;
                break;
            case 2: //入流水队
                $data['pl_start'] = $time;
                $data['work_pipe'] = $work_pipe;
                break;
            case 3: //挂起
                $data['hu_start'] = $time;
                break;
            case 4: //完成
            case 5: //作费
                break;
            default:
                break;
        }

        $this->edit_work($work_id, $data);
    }

    /**
     * 获取当天的明细统计数据
     * @return array
     */
    public function statistica_detailed()
    {
        $today_start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $sql = "SELECT w.work_status, wi.wi_title, wi.wi_num, wi.wi_unit FROM " . XS_WORK . " w, " . XS_WORKINFO . " wi WHERE w.work_id = wi.work_id AND w.add_time >= ?";
        $query = $this->db->query($sql, array($today_start));
        $statistica = array();
        if ($query->num_rows() > 0) {
            $rows_tmp = $query->result_array();
            foreach ($rows_tmp as $row) {
                if (!isset($statistica[$row['wi_title']])) {
                    $statistica[$row['wi_title']] = array(
                        'title' => $row['wi_title'],
                        'unit' => $row['wi_unit'],
                        'all_num' => 0,
                        'wait_num' => 0,
                        'already_num' => 0
                    );
                }
                $statistica[$row['wi_title']]['all_num'] += $row['wi_num'];
                if ($row['work_status'] == 4) {
                    $statistica[$row['wi_title']]['already_num'] += $row['wi_num'];
                } else if ($row['work_status'] != 5) {
                    $statistica[$row['wi_title']]['wait_num'] += $row['wi_num'];
                }
            }
        }
        usort($statistica, "fun_usort");

        return $statistica;
    }

    /**
     * 获取当前已完成的最大订单号
     */
    public function get_max_order_id()
    {
        $max_order_id = 0;
        $sql = "SELECT MAX(order_id) max_order_id FROM " . XS_WORK . " WHERE work_status = 4";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $rows_tmp = $query->row_array();
            $max_order_id = $rows_tmp['max_order_id'];
        }

        return $max_order_id;
    }
}